package com.karaku.karaku_collector.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;

import com.karaku.karaku_collector.App;
import com.karaku.karaku_collector.util.ClassUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by guoping on 17/4/25.
 */
public class KarakuActivityManager {

    private static int frontCount = 0;
    private static List<Activity> activities = new LinkedList<>();

    public synchronized static void addActivity(Activity activity) {
        if (!activities.contains(activity)) {
            activities.add(activity);
        }
    }

    public synchronized static void removeActivity(Activity activity) {
        if (activities.contains(activity)) {
            activities.remove(activity);
        }
    }

    public synchronized static boolean isEmpty() {
        return activities.isEmpty();
    }

    public static List<Activity> getActivities() {
        return activities;
    }

    public synchronized static boolean hasActivity(Class<? extends Activity> activityClass) {
        for (Activity a : activities) {
            if (a.getClass().equals(activityClass)) {
                return true;
            }
        }
        return false;
    }

    public static void finishAllWithout(Class<? extends Activity>... classes) {
        List<Activity> as = new ArrayList<>(activities);
        for (Activity a : as) {
            boolean contains = false;
            for (Class c : classes) {
                if (a.getClass().equals(c)) {
                    contains = true;
                    break;
                }
            }
            if (contains) {
                continue;
            }
            a.finish();
        }
    }

    public static void gotoAndClearTop(Class<? extends Activity> activityClass) {
        startNew(activityClass, Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    public synchronized static Activity lastOrDefault() {
        if (activities.size() > 0) {
            return activities.get(activities.size() - 1);
        } else {
            return null;
        }
    }

    public static void startForResult(Activity context, Class<? extends Activity> activityClass, int reqCode) {
        if (ClassUtil.isSubClass(activityClass, Activity.class)) {
            Intent intent = new Intent(App.getInstance(), activityClass);
            context.startActivityForResult(intent, reqCode);
        }
    }

    public static void startNew(Class<? extends Activity> activityClass, Object...params) {
        startNew(activityClass, null, params);
    }

    public static void startNew(Class<? extends Activity> activityClass, Integer flags, Object...params) {
        if (ClassUtil.isSubClass(activityClass, Activity.class)) {
            Intent intent = new Intent(App.getInstance(), activityClass);
            if (flags != null) {
                intent.addFlags(flags);
            }
            if (params != null && params.length > 0 && params.length % 2 == 0) {
                for (int i = 0; i < params.length; i += 2) {
                    String key = (String) params[i];
                    Object value = params[i + 1];
                    if (value == null) {
                        continue;
                    }
                    if (ClassUtil.isSubClass(value.getClass(), Integer.class)) {
                        intent.putExtra(key, (Integer) value);
                    } else if (ClassUtil.isSubClass(value.getClass(), Long.class)) {
                        intent.putExtra(key, (Long) value);
                    } else if (ClassUtil.isSubClass(value.getClass(), Float.class)) {
                        intent.putExtra(key, (Float) value);
                    } else if (ClassUtil.isSubClass(value.getClass(), Double.class)) {
                        intent.putExtra(key, (Double) value);
                    } else if (ClassUtil.isSubClass(value.getClass(), Boolean.class)) {
                        intent.putExtra(key, (Boolean) value);
                    } else if (ClassUtil.isSubClass(value.getClass(), Serializable.class)) {
                        intent.putExtra(key, (Serializable) value);
                    } else if (ClassUtil.isSubClass(value.getClass(), Parcelable.class)) {
                        intent.putExtra(key, (Parcelable) value);
                    } else {
                        intent.putExtra(key, value.toString());
                    }
                }
            }
            startNew(intent);
        }
    }

    public static void startNew(Intent intent) {
        Context context;
        if (!isEmpty()) {
            context = lastOrDefault();
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context = App.getInstance().getApplicationContext();
        }
        context.startActivity(intent);
    }

    public static void onResume(Activity activity) {
        frontCount++;
    }

    public static void onPause(Activity activity) {
        frontCount--;
    }

    public static boolean appIsFront() {
        return frontCount > 0;
    }
}
